import { BaseItem } from "../../common/BaseItem";
import { CopyRewardRollItem } from "./CopyRewardRollItem";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { FlyToNavbarTool } from "../../award/FlyToNavbarTool";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { EProp } from "../../../../../Message/Public/EProp";
import { GameProxy } from "../../../mvc/GameProxy";
import { GuideTipsManager } from "../../../manager/GuideTipsManager";
import { FrameTimer } from "../../../../../com/gengine/core/frame/FrameTimer";
import { TimerType } from "../../../../../com/gengine/core/frame/TimerType";
import { DataEvent } from "../../../events/DataEvent";
import { ObjCreate } from "../../common/ObjCreate";
import { ImagesConst } from "../../../resource/ImagesConst";
import { Cache } from "../../../cache/Cache";
import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ImageInfo } from "../../../../../com/gengine/resource/info/ImageInfo";
import { RemainTimeTool } from "../../award/RemainTimeTool";
import { Alert } from "../../../../../com/mui/controls/Alert";
import { ItemData } from "../../../resource/info/ItemData";
import { SReward } from "../../../../../Message/Public/SReward";
import { GLabel } from "../../../../../com/mui/controls/GLabel";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { Vector } from "../../../../../__AS3__/vec/Vector";
type int = number;
//class CopyDrawRewardComp
    
    export  class CopyDrawRewardComp /* flash.display.Sprite */
    {
        constructor(arg1: Boolean=false)
        {
            this._drawItems = new Vector.<BaseItem>();
            this._rollItems = new Vector.<CopyRewardRollItem>();
            this._positionDic = [{"x": 147, "y": 58}, {"x": 207, "y": 83}, {"x": 230, "y": 139}, {"x": 207, "y": 198}, {"x": 147, "y": 225}, {"x": 87, "y": 198}, {"x": 63, "y": 139}, {"x": 87, "y": 83}];
            this._showAutoNext = arg1;
            
            this.createChildren();
            return;
        }

        /* internal  */playFlyToBackPack(arg1: CopyRewardRollItem): void
        {
            var loc1=arg1.lastPos;
            arg1.setPlayEfffect(this._drawItems[loc1].itemData.color);
            MsgManager.showRollTipsMsg(Language.getStringByParam(60062, this._drawItem.itemInfo.item.name, this._drawItems[loc1].itemData.name));
            FlyToNavbarTool.flyToBackPack(this._drawItems[loc1].bitmapdata, this._drawItems[loc1].localToGlobal(new Object /* flash.geom.Point */(16, 16)));
            return;
        }

        public playShowEffect(): void
        {
            if (this._showEffect) 
            {
                this.onLoadedShowEffect();
            }
            else 
            {
                LoaderManager.instance.load("DrawRewardBgShowEffect.swf", this.onLoadedShowEffect);
            }
            return;
        }

        /* internal  */onLoadedShowEffect(arg1: SWFInfo=null): void
        {
            if (arg1) 
            {
                this._showEffect = arg1.clip;
            }
            this._showEffect.x = -34;
            this._showEffect.y = -38;
            this._showEffect.addEventListener(flash.events.Event.ENTER_FRAME, this.onShowEffectEnterframe);
            this.addChild(this._showEffect);
            this._showEffect.gotoAndPlay(1);
            return;
        }

        /* internal  */onShowEffectEnterframe(arg1: Object /* flash.events.Event */): void
        {
            if (this._showEffect.currentFrame == this._showEffect.totalFrames) 
            {
                this._showEffect.removeEventListener(flash.events.Event.ENTER_FRAME, this.onShowEffectEnterframe);
                if (this._showEffect.parent) 
                {
                    this._showEffect.parent.removeChild(this._showEffect);
                }
            }
            return;
        }

        public playStartEffect(): void
        {
            if (this._startEffect) 
            {
                this.onLoadedStartEffect();
            }
            else 
            {
                LoaderManager.instance.load("StartDrawRewardEffect.swf", this.onLoadedStartEffect);
            }
            return;
        }

        /* internal  */onLoadedStartEffect(arg1: SWFInfo=null): void
        {
            if (arg1) 
            {
                this._startEffect = arg1.clip;
            }
            this._startEffect.x = -34;
            this._startEffect.y = -42;
            this._startEffect.addEventListener(flash.events.Event.ENTER_FRAME, this.onStartEffectEnterframe);
            this.addChild(this._startEffect);
            this._startEffect.gotoAndPlay(1);
            return;
        }

        public openDrawItem(): void
        {
            if (this.hadDraw) 
            {
                return;
            }
            this.playStartEffect();
            if (this._drawItem && this._drawItem.type == EProp._EPropCopyBox) 
            {
                GameProxy.copy.openCopyBox(this._drawItem.uid, true, this.onOpenCopyBoxSuccess);
            }
            else if (this._drawItem && this._drawItem.type == EProp._EPropHappyBag) 
            {
                GameProxy.happyBag.openHappyBag(this._drawItem.uid, this.onOpenCopyBoxSuccess);
            }
            if (GuideTipsManager.guideTips.parent == this) 
            {
                GuideTipsManager.hideGuideTips();
            }
            this.dispatchEvent(new Object /* flash.events.Event */(CopyDrawRewardComp.OPEN_COPYBOX_SUCCESS));
            return;
        }

        /* internal  */onStartEffectEnterframe(arg1: Object /* flash.events.Event */): void
        {
            if (this._startEffect.currentFrame == this._startEffect.totalFrames) 
            {
                this._startEffect.removeEventListener(flash.events.Event.ENTER_FRAME, this.onStartEffectEnterframe);
                if (this._startEffect.parent) 
                {
                    this._startEffect.parent.removeChild(this._startEffect);
                }
            }
            return;
        }

        public startDraw(): void
        {
            this.addRollItems();
            if (!this._frame) 
            {
                this._frame = new FrameTimer();
                this._frame.addListener(TimerType.ENTERFRAME, this.onEnterFrameHandler);
            }
            this._frame.start();
            return;
        }

        /* internal  */onEnterFrameHandler(arg1: FrameTimer): void
        {
            this.updateRollItemsPosition();
            if (this.isComplete()) 
            {
                if (this._frame && this._frame.running) 
                {
                    this._frame.stop();
                }
                this._startBtn.visible = true;
                this._endBtn.visible = false;
                this.setFastRun(false);
                this.dispatchEvent(new DataEvent(CopyDrawRewardComp.DrawComplete, this._showAutoNext && this._autoNext.selected));
            }
            return;
        }

        /* internal  */updateRollItemsPosition(): void
        {
            var loc1=0;
            var loc2=0;
            loc1 = 0;
            while (loc1 < this._rollItems.length) 
            {
                if (this._rollItems[loc1] && this._rollItems[loc1].running) 
                {
                    this._rollItems[loc1].currentCount = this._rollItems[loc1].currentCount + 1;
                    if (this._frame.currentCount % this._rollItems[loc1].speed == 0) 
                    {
                        loc2 = this._rollItems[loc1].position;
                        this._rollItems[loc1].x = this._positionDic[loc2]["x"];
                        this._rollItems[loc1].y = this._positionDic[loc2]["y"];
                    }
                }
                ++loc1;
            }
            return;
        }

        /* internal  */stopAndGetReward(): void
        {
            this.setFastRun(true);
            return;
        }

        /* internal  */setFastRun(arg1: Boolean): void
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < this._rollItems.length) 
            {
                if (this._rollItems[loc1] && this._rollItems[loc1].running) 
                {
                    this._rollItems[loc1].fastRun = arg1;
                }
                ++loc1;
            }
            return;
        }

        /* internal  */addRollItems(): void
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < this._rollItems.length) 
            {
                this.addChild(this._rollItems[loc1]);
                ++loc1;
            }
            return;
        }

        /* internal  */removeRollItems(): void
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < this._rollItems.length) 
            {
                if (this._rollItems[loc1].parent) 
                {
                    this._rollItems[loc1].parent.removeChild(this._rollItems[loc1]);
                    this._rollItems[loc1].destroy();
                    this._rollItems[loc1] = null;
                }
                ++loc1;
            }
            this._rollItems = new Vector.<CopyRewardRollItem>();
            return;
        }

        /* internal  */clearDrawItems(): void
        {
            var loc1=0;
            while (loc1 < this._drawItems.length) 
            {
                this._drawItems[loc1].itemData = null;
                ++loc1;
            }
            return;
        }

        protected createChildren(): void
        {
            var loc1=null;
            this._background = new Object /* flash.display.Bitmap */();
            this.addChild(this._background);
            LoaderManager.instance.load("DrawRewardBg.png", this.onBgLoaded);
            this._titleLabel = ObjCreate.createLabel("", 0, 8, 328, 20, flash.text.TextFormatAlign.CENTER, this, true, 16776960);
            this.createDrawItems();
            if (this._showAutoNext) 
            {
                ObjCreate.createBg(114, 148, 105, 22, this, ImagesConst.SelectedBg);
                this._autoNext = ObjCreate.createCheckBox(Language.getString(60436), 114, 147, 105, 20, this);
                this._autoNext.selected = true;
            }
            this._countDownLabel = ObjCreate.createLabel("", 220, 320, 100, 20, flash.text.TextFormatAlign.LEFT, this, true, 16776960);
            this._closeBtn = ObjCreate.createGButton("", 250, 10, 19, 19, this, "CloseButton");
            this._closeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCloseHandler);
            this._closeBtn.focusEnabled = true;
            this._startBtn = ObjCreate.createGButton("", 137, 280, 52, 52, this, "DrawRewardBtn1");
            this._startBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onStartDrawBtnClick);
            this._endBtn = ObjCreate.createGButton("", 137, 280, 52, 52, this, "DrawRewardBtn2");
            this._endBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onStartDrawBtnClick);
            this._endBtn.drawNow();
            this._endBtn.visible = false;
            if (Cache.instance.guide.guideCopyLuckDraw) 
            {
                loc1 = GuideTipsManager.getGuideTips(GuideTipsManager.Dir_BL, Language.getString(60058));
                loc1.x = this._startBtn.x + 52;
                loc1.y = this._startBtn.y + 20;
                this.addChild(loc1);
            }
            return;
        }

        /* internal  */createDrawItems(): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=0;
            while (loc3 < this._positionDic.length) 
            {
                loc1 = GlobalClass.getBitmap(ImagesConst.PackItemBg);
                loc1.x = this._positionDic[loc3]["x"];
                loc1.y = this._positionDic[loc3]["y"];
                this.addChild(loc1);
                loc2 = new BaseItem();
                loc2.setSize(32, 32);
                loc2.x = loc1.x + 3;
                loc2.y = loc1.y + 3;
                this.addChild(loc2);
                this._drawItems.push(loc2);
                ++loc3;
            }
            return;
        }

        /* internal  */onBgLoaded(arg1: ImageInfo): void
        {
            this._background.bitmapData = arg1.bitmapData;
            return;
        }

        public setTitleLabel(arg1: string): void
        {
            this._titleLabel.htmlText = arg1;
            return;
        }

        public setCountDown(arg1: int): void
        {
            if (arg1 < 0) 
            {
                this._countDownLabel.htmlText = "";
            }
            else 
            {
                this._countDownLabel.htmlText = RemainTimeTool.getRemainTimeBySeconds(arg1);
            }
            return;
        }

        /* internal  */onCloseHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._drawItem.type == EProp._EPropHappyBag && this.hadDraw && !this.isComplete()) 
            {
                Alert.buttonWidth = 75;
                Alert.okLabel = Language.getString(60059);
                Alert.calcelLabel = Language.getString(60060);
                Alert.show(Language.getStringByParam(60061, this._drawItem.itemInfo.item.name), Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onCloseAlertHandler);
            }
            else 
            {
                this.onClose();
            }
            return;
        }

        public isDrawing(): Boolean
        {
            return this.hadDraw && !this.isComplete();
        }

        /* internal  */onCloseAlertHandler(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.onClose();
            }
            return;
        }

        /* internal  */onClose(): void
        {
            this.hide();
            this.dispatchEvent(new Object /* flash.events.Event */(CopyDrawRewardComp.CLOSE_COPYBOX));
            return;
        }

        public hide(): void
        {
            if (this._frame && this._frame.running) 
            {
                this._frame.stop();
            }
            this.clearDrawItems();
            this.removeRollItems();
            return;
        }

        /* internal  */onStartDrawBtnClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=flash.utils.getTimer();
            if (loc1 - this._lasttime < 500) 
            {
                return;
            }
            this._lasttime = loc1;
            if (this.hadDraw && !this.isComplete()) 
            {
                this.stopAndGetReward();
            }
            else 
            {
                this.openDrawItem();
            }
            return;
        }

        public isComplete(): Boolean
        {
            var loc1=0;
            loc1 = 0;
            while (loc1 < this._rollItems.length) 
            {
                if (this._rollItems[loc1].running) 
                {
                    return false;
                }
                ++loc1;
            }
            return true;
        }

        /* internal  */onOpenCopyBoxSuccess(arg1, arg2: Map<any, any> /* flash.utils.Dictionary */): void
        {
            var loc2=0;
            var loc3=null;
            this.hadDraw = true;
            this._startBtn.visible = false;
            this._endBtn.visible = true;
            var loc1=[];
            var loc4=0;
            var loc5=arg2;
            for (loc3 in loc5) 
            {
                loc2 = parseInt(loc3);
                loc1.push(loc2);
            }
            this.countRewardAndStartDraw(loc1);
            return;
        }

        /* internal  */countRewardAndStartDraw(arg1: Array<any>): void
        {
            var loc1=0;
            var loc2=0;
            this.setRawardNum(arg1.length);
            loc1 = 0;
            while (loc1 < arg1.length) 
            {
                loc2 = 0;
                while (loc2 < this._drawItems.length) 
                {
                    if (this._drawItems[loc2].itemData.itemCode == arg1[loc1]) 
                    {
                        this._rollItems[loc1].lastPos = loc2;
                        break;
                    }
                    ++loc2;
                }
                ++loc1;
            }
            this.startDraw();
            return;
        }

        public set drawItem(arg1: ItemData)
        {
            this.setTitleLabel(arg1.itemInfo.item.name);
            this.hadDraw = false;
            this.removeRollItems();
            this.clearDrawItems();
            this._drawItem = arg1;
            if (this._drawItem && this._drawItem.type == EProp._EPropCopyBox) 
            {
                GameProxy.copy.showOpenCopyBox(this._drawItem.itemCode, this.onShowDrawItemsSuccess);
            }
            else if (this._drawItem && this._drawItem.type == EProp._EPropHappyBag) 
            {
                GameProxy.happyBag.showOpenHappyBag(this._drawItem.uid, this.onShowDrawItemsSuccess);
            }
            return;
        }

        public onShowDrawItemsSuccess(arg1, arg2: Array<any>): void
        {
            this.setRewards(arg2);
            return;
        }

        public setRewards(arg1: Array<any>): void
        {
            var loc1=null;
            var loc2=0;
            while (loc2 < arg1.length) 
            {
                loc1 = arg1[loc2] as SReward;
                this._drawItems[loc2].itemData = new ItemData(loc1.code);
                ++loc2;
            }
            return;
        }

        public setRawardNum(arg1: int): void
        {
            var loc1=0;
            var loc2=null;
            this.removeRollItems();
            loc1 = 0;
            while (loc1 < arg1) 
            {
                loc2 = new CopyRewardRollItem(loc1);
                loc2.currentCount = loc1;
                loc2.itemsNum = this._drawItems.length;
                loc2.addEventListener(CopyRewardRollItem.RUNNING_COMPLETE, this.onRunningComplete);
                this._rollItems.push(loc2);
                ++loc1;
            }
            return;
        }

        /* internal  */onRunningComplete(arg1: Object /* flash.events.Event */): void
        {
            var loc1=arg1.target as CopyRewardRollItem;
            this.playFlyToBackPack(loc1);
            return;
        }

        public static /* const */OPEN_COPYBOX_SUCCESS: string="成功开启抽奖宝箱";

        public static /* const */CLOSE_COPYBOX: string="关闭抽奖";

        public static /* const */DrawComplete: string="抽奖结束";

        /* internal */ /* var */_showAutoNext: Boolean;

        /* internal */ /* var */_background: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_showEffect: Object /* flash.display.MovieClip */;

        /* internal */ /* var */_startEffect: Object /* flash.display.MovieClip */;

        public /* var */hadDraw: Boolean=false;

        /* internal */ /* var */_drawItem: ItemData;

        /* internal */ /* var */_rollItems: Vector.<CopyRewardRollItem>;

        /* internal */ /* var */_titleLabel: GLabel;

        /* internal */ /* var */_countDownLabel: GLabel;

        /* internal */ /* var */_closeBtn: GButton;

        /* internal */ /* var */_startBtn: GButton;

        /* internal */ /* var */_endBtn: GButton;

        /* internal */ /* var */_autoNext: GCheckBox;

        /* internal */ /* var */_frame: FrameTimer;

        /* internal */ /* var */_positionDic: Array<any>;

        /* internal */ /* var */_lasttime: int;

        /* internal */ /* var */_drawItems: Vector.<BaseItem>;
    }
